import type { GreasyForkUserConfig } from '../../../../../../lib/page_handler/platform/greasyfork/types'
import type { PlatformTypeValue } from '../../../../../../types/interfaces'

export interface UserAnalyticsConfig extends GreasyForkUserConfig {}
export type UserAnalyticsPlatformConfig = Partial<Record<PlatformTypeValue, Record<string, UserAnalyticsConfig>>>

export type UserAnalyticsPostRatingType = 'good' | 'ok' | 'bad' | 'none'
export type UserAnalyticsScriptListType = 'good' | 'bad' | 'totalRatings' | 'totalInstalls' | 'dailyInstalls' | 'zeroBadRating' | 'zeroInstall' | 'unrated'
export type UserAnalyticsActivityType = 'script-created' | 'script-updated' | 'post-created' | 'reply-created'

export const USER_ANALYTICS_MAX_COMMENT_PAGES = 15 // 最大获取页数

export interface UserAnalyticsScriptData {
  id: string
  name: string
  url: string
  createdDate: number
  updatedDate: number
  dailyInstalls: number
  totalInstalls: number
  totalRatings: number
  ratings: {
    good: number
    ok: number
    bad: number
  }
}

export enum UserAnalyticsCommentRole {
  Answerer = 'answerer',
  OriginalPoster = 'original-poster',
  Author = 'author',
}

export interface UserAnalyticsCommentData {
  id: string
  url: string
  scriptName: string
  scriptUrl: string
  role: UserAnalyticsCommentRole
  rating: UserAnalyticsPostRatingType
  content: string
  timestamp: number
  pageNum: number
  positionInPage: number
}

export interface UserAnalyticsUserData {
  name: string
  profileUrl: string
  joinDate?: string
}

export interface UserAnalyticsCollectedData {
  scripts: UserAnalyticsScriptData[]
  comments: UserAnalyticsCommentData[]
  userInfo: UserAnalyticsUserData
}

export interface UserAnalyticsScriptTotalInstallRanges {
  '0': number
  '0-1k': number
  '1k-1w': number
  '1w-5w': number
  '5w-10w': number
  '10w-100w': number
  '100w-500w': number
  '500w-1m': number
  '1m+': number
}

export interface UserAnalyticsScriptDailyInstallRanges {
  '0': number
  '0-50': number
  '50-80': number
  '80-100': number
  '100-150': number
  '150-200': number
  '200+': number
}

export interface UserAnalyticsCreationTimeDistribution {
  byYear: Record<string, number>
}

export interface UserAnalyticsScriptRatingDistribution {
  good: number
  ok: number
  bad: number
}

export interface UserAnalyticsCommentScriptDistribution {
  [scriptName: string]: {
    count: number
    timestamp: number
  }
}

export interface UserAnalyticsCommentRatingDistribution {
  good: number
  ok: number
  bad: number
  none: number
}

export interface UserAnalyticsCommentTimeDistribution {
  byWeekday: number[]
  byHour: number[]
}

export interface UserAnalyticsActivity {
  type: UserAnalyticsActivityType
  timestamp: number
  scriptName: string
  scriptUrl: string
  commentUrl?: string
  content?: string
  rating?: UserAnalyticsPostRatingType
}

export interface UserAnalyticsData {
  // 脚本统计
  totalScripts: number
  scriptTotalInstallRanges: UserAnalyticsScriptTotalInstallRanges
  scriptDailyInstallRanges: UserAnalyticsScriptDailyInstallRanges
  allTotalInstalls: number
  allDailyInstalls: number
  maxDailyInstalls: number
  maxTotalInstalls: number
  topDailyInstalls: UserAnalyticsScriptData[]
  topTotalInstalls: UserAnalyticsScriptData[]
  zeroInstallScripts: UserAnalyticsScriptData[]
  zeroBadRatingScripts: UserAnalyticsScriptData[]

  // 时间分布
  firstCreation: UserAnalyticsScriptData
  lastCreation: UserAnalyticsScriptData
  firstUpdate: UserAnalyticsScriptData
  lastUpdate: UserAnalyticsScriptData
  creationTimeDistribution: UserAnalyticsCreationTimeDistribution
  updateTimeDistribution: Record<string, number> // 星期分布
  updateHourDistribution: Record<string, number> // 24小时分布

  // 脚本评价统计
  maxScriptRating: number
  scriptRatingDistribution: UserAnalyticsScriptRatingDistribution
  topRatedScripts: UserAnalyticsScriptData[]
  topBadRatedScripts: UserAnalyticsScriptData[]
  mostRatedScripts: UserAnalyticsScriptData[]
  unratedScripts: UserAnalyticsScriptData[]

  // 用户留言统计
  commentPageNum: number
  totalComments: number
  totalAnswererComments: number
  totalCommentScript: number
  totalPostScript: number
  commentScriptGoodRating: number
  commentScriptBadRating: number
  commentScriptDistribution: UserAnalyticsCommentScriptDistribution
  commentRatingDistribution: UserAnalyticsCommentRatingDistribution
  firstComment: UserAnalyticsCommentData
  firstOriComment: UserAnalyticsCommentData
  firstAnswererComment: UserAnalyticsCommentData
  lastComment: UserAnalyticsCommentData
  commentTimeDistribution: UserAnalyticsCommentTimeDistribution

  // 时间指标
  daysSinceLastScriptUpdate?: number
  daysSinceLastComment?: number

  // 用户活跃度
  commentFrequency: number // 条/天
  updateFrequency: number // 次/天
  recentActivities?: UserAnalyticsActivity[]
}

export interface UserAnalyticsCommentStatus {
  type: 'no-comments' | 'no-ratings' | 'has-data'
  message: string
  description: string
  className: string
}

export interface UserAnalyticsMultiPageOptions {
  maxPages: number
  startPage: number
  loadAll: boolean
}

export const INSTALL_RANGE_COLORS = {
  // 总安装量配色（从低到高，蓝色渐变）
  TOTAL_INSTALL: {
    '0': '#C0C0C0', // 灰色 - 零安装
    '0-1k': '#9EC8FA', // 浅蓝 - 微量安装
    '1k-1w': '#6BA4F7', // 淡蓝 - 小量安装
    '1w-5w': '#3B82F6', // 标准蓝 - 中等安装
    '5w-10w': '#2563EB', // 深蓝 - 大量安装
    '10w-100w': '#1D4ED8', // 深蓝 - 超大量安装
    '100w-500w': '#1E40AF', // 深蓝 - 超大量安装
    '500w-1m': '#1E3A8A', // 深蓝 - 超大量安装
    '1m+': '#0C4A6E', // 深蓝 - 超大量安装
  },

  // 日安装量配色（从低到高，绿色渐变）
  DAILY_INSTALL: {
    '0': '#C0C0C0', // 灰色 - 零安装
    '0-50': '#BBF7D0', // 浅绿 - 低安装
    '50-80': '#86EFAC', // 淡绿 - 中低安装
    '80-100': '#4ADE80', // 标准绿 - 中等安装
    '100-150': '#22C55E', // 深绿 - 中高安装
    '150-200': '#16A34A', // 深绿 - 高安装
    '200+': '#15803D', // 深绿 - 超高安装
  },
}

/**
 * 获取安装量范围的颜色
 */
export function getInstallRangeColor(range: string, type: 'total' | 'daily'): string {
  const colorMap = type === 'total' ? INSTALL_RANGE_COLORS.TOTAL_INSTALL : INSTALL_RANGE_COLORS.DAILY_INSTALL
  return colorMap[range as keyof typeof colorMap] || '#64748B' // 默认颜色
}
